package com.example.config;

import com.example.service.OnlineUserTrackerService;
import com.example.service.impl.OnlineUserTrackerServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class OnlineUserConfig {

    @Value("${app.online.ttl-seconds:1800}")
    private long ttlSeconds;

    @Bean
    // @ConditionalOnBean(StringRedisTemplate.class)
    // 踩到的一个坑：如果加上以上这个注解，会与RedisConfig.java中的 @ConditionalOnBean(StringRedisTemplate.class)
    // 产生依赖死锁
    @ConditionalOnProperty(prefix = "app.redis", name = "enabled", havingValue = "true")
    public OnlineUserTrackerService onlineUserTracker(StringRedisTemplate stringRedisTemplate) {
        return new OnlineUserTrackerServiceImpl(stringRedisTemplate, ttlSeconds);
    }
}



